﻿var focusPoint = Math.floor(Math.random()*501) + 50;                       //focusPoint will be randomised between 25 and 575 every time the microscope is opened or the slide is changed, is the focus point for 1000x.
var neg100 = (Math.floor(Math.random()*2) == 1 ) ? -1 : 1;                 //Randomised between 1 and -1, showing if 100x point of focus is above or below the value of focusPoint.
var neg400 = (Math.floor(Math.random()*2) == 1 ) ? -1 : 1;                 //Randomised between 1 and -1, showing if 400x point of focus is above or below the value of focusPoint.
var focus100 = Math.floor(Math.random()*51) * neg100 + focusPoint;         //Point of focus for 100x magnification (which the valid range centres around), within a certain range of the the value of focusPoint.
var focus400 = Math.floor(Math.random()*25) * neg400 + focusPoint;         //Point of focus for 400x magnification (which the valid range centres around), within a certain range of the the value of focusPoint.		
var focus = 0;                                                             //The focus must be within a certain range of the focus point range of a magnification level to get a focused image.
var oil = false;                                                           //Boolean indicating whether oil has been applied to the microscope or not.
var mag = 0;                                                               //Integer representing magnification level: 0 = 100x; 1 = 400x; 2 = 1000x.
var light = false;                                                         //Boolean indicating whether the light is on or not.
var const_COARSEINC = 20;                                                  //Constant number, how much the focus value will increment or decrement when the coarse control is used.
var const_FINEINC = 1;                                                     //Constant number, how much the focus value will increment or decrement when the fine control is used.
var const_OILBLUR = 3;                                                     //Constant number, shows how much the microscope will blur if oil is applied inappropriately.
var const_100RANGE = 50;                                                   //Constant number, range around focusPoint the focus must fall in to be focused properly at 100x.
var const_400RANGE = 15;                                                   //Constant number, range around focusPoint the focus must fall in to be focused properly at 400x.
var const_1000RANGE = 0;                                                   //Constant number, range around focusPoint the focus must fall in to be focused properly at 1000x.
var const_100INC = 0.01;                                                   //Constant number, indicates how much the microscope will blur for each increment of focus away from the range at 100x.
var const_400INC = 0.05;                                                   //Constant number, indicates how much the microscope will blur for each increment of focus away from the range at 400x.
var const_1000INC = 0.1;                                                   //Constant number, indicates how much the microscope will blur for each increment of focus away from the range at 1000x.
var labels = false;                                                        //Boolean indicating if labels are turned on.
var maglabel1 = "10x";                                                     //Label used next to slide title at 100x magnification.
var maglabel2 = "40x";                                                     //Label used next to slide title at 400x magnification.
var maglabel3 = "100x";                                                    //Label used next to slide title at 1000x magnification.
var hintdisp = false;                                                      //Boolean indicating whether a hint should be displayed.
var guidedisp = false;                                                     //Boolean indicating whether the guide should be displayed.


$(document).ready(function()
{
	window.imagevar = document.getElementById("image");
	window.imagevar1 = document.getElementById("image1");
	window.imagevar2 = document.getElementById("image2");
}
)

// Function to display the labels on the microscope.
function addLabels()
{
	if (labels)
	{
		$("#labelled").hide();
		$("#microscope").show();
		labels = false;
	}
	else 
	{
		$("#microscope").hide();
		$("#labelled").show();
		labels = true;
	}
}

//Function that increases the coarse focus level.
function coarseInc()
{
	if (focus < 581) focus += const_COARSEINC;
	else alert("You can't increase the coarse focus any more.");
	renderImage();
}

//Function that decreases the coarse focus level.
function coarseDec()
{
	if (focus > 19) focus -= const_COARSEINC;
	else alert("You can't decrease the coarse focus any more.");
	renderImage();
}

//Function that increases the fine focus level.
function fineInc()
{
	if (focus < 600) focus += const_FINEINC;
	else alert("You can't increase the fine focus any more.");
	renderImage();
}

//Function that decreases the fine focus level.
function fineDec()
{
	if (focus > 0) focus -= const_FINEINC;
	else alert("You can't decrease the fine focus any more.");
	renderImage();
}

//Function that toggles the light on and off.		
function toggleLight()
{
	if (light) light = false;
	else light = true;
	renderImage();
}

// Function that adds oil.
function addOil()
{
	oil = true;
	renderImage();
}

//Function that removes oil.
function removeOil()
{
	oil = false;
	renderImage();
}

function incMag()
{
	if (mag < 2) mag += 1;
	else alert("You can't increase the magnification level any more.");
	renderImage();
}

function decMag()
{
	if (mag > 0) mag -= 1;
	else alert("You can't decrease the magnification level any more.");
	renderImage();
}


//Function that sets magnification to 100x.
function mag100()
{
	mag = 0;
	renderImage();
}

//Function that sets magnification to 400x.
function mag400()
{
	mag = 1;
	renderImage();
}

//Function that sets magnification to 1000x.
function mag1000()
{
	mag = 2;
	renderImage();
}

function hinthide(){
	hintdisp = false;
	$("#hint").slideUp("fast");
}

function guidehide(){
	guidedisp = false;
	$("#guide").slideUp("fast");
}

/*	This function will process all of the variables affected by the other functions and draw the image accordingly.
	Note that you cannot change the image if there is a Pixastic blur effect on it, so the image must be reverted before each image change attempt.
	Blur is also reverted before another blur effect takes place because the blur effect should be dependent of the original image, not on the previously blurred image.
	Blurring is determined by distance from the focus value to the magnification dependent range centred around the focusPoint, if the focus value is within the range, the image will be clear.
*/
function renderImage()
{
		if (mag == 0)$("#mag").text(maglabel1);	
		if (mag == 1)$("#mag").text(maglabel2);
		if (mag == 2)$("#mag").text(maglabel3);	
		if (guidedisp){
			updateguide();
		}

	if (hintdisp == true){
		hinthide();
	}

	
	if(light != true)
	{
		//document.getElementById("image").src="FrontEnd_files/noslide.jpg";
		$("#image").hide();	
		$("#image1").hide();	
		$("#image2").hide();
				
	}
	else
	{
		if(mag == 0)
		{			
			$("#image1").hide();
			$("#image2").hide();
			$("#image").show();
			
			
			if(oil)			//If there is oil, blur the image by a fixed constant.
			{
				Pixastic.revert(imagevar);
	        	Pixastic.process(imagevar, "blurfast", {amount:const_OILBLUR});
			}
			else			//Else blur the image according to how close the focus level is to the focus range for 100x.
			{
				Pixastic.revert(document.getElementById("image"));
				if (focus > focus100 + const_100RANGE)
				{
	        		Pixastic.process(imagevar, "blurfast", {amount:(focus - (focus100 + const_100RANGE)) * const_100INC});
				}
				else
				{
					if (focus < focus100 - const_100RANGE)
					{
	        			Pixastic.process(imagevar, "blurfast", {amount:((focus100 - const_100RANGE) - focus) * const_100INC});
					}
				}
			}
		}
		if(mag == 1)
		{
			$("#image").hide();
			$("#image2").hide();
			$("#image1").show();
			
			if(oil)			//If there is oil, blur the image by a fixed constant.
			{
				Pixastic.revert(document.getElementById("image1"));
	        	Pixastic.process(document.getElementById("image1"), "blurfast", {amount:const_OILBLUR});
			}
			else		//Else blur the image according to how close the focus level is to the focus range for 400x.
			{
				Pixastic.revert(document.getElementById("image1"));
				
				if (focus > focus400 + const_400RANGE)
				{
	        		Pixastic.process(document.getElementById("image1"), "blurfast", {amount:(focus - (focus400 + const_400RANGE)) * const_400INC});
				}
				else
				{
					if (focus < focus400 - const_400RANGE)
					{
	        			Pixastic.process(document.getElementById("image1"), "blurfast", {amount:((focus400 - const_400RANGE) - focus) * const_400INC});
					}
				}

			}
		}
		if(mag == 2)
		{
			$("#image").hide();
			$("#image1").hide();
			$("#image2").show();
			
			if(oil != true)		//If there is no oil, blur the image by a fixed constant.
			{
				Pixastic.revert(document.getElementById("image2"));
	        	Pixastic.process(document.getElementById("image2"), "blurfast", {amount:const_OILBLUR});
			}
			else		//Else blur the image according to how close the focus level is to the focus range for 1000x (exact).
			{
				Pixastic.revert(document.getElementById("image2"));
				if (focus > focusPoint + const_1000RANGE)
				{
	        		Pixastic.process(document.getElementById("image2"), "blurfast", {amount:(focus - (focusPoint + const_1000RANGE)) * const_1000INC});
				}
				else
				{
					if (focus < focusPoint - const_1000RANGE)
					{
	        			Pixastic.process(document.getElementById("image2"), "blurfast", {amount:((focusPoint - const_1000RANGE) - focus) * const_1000INC});
					}
				}

			}
		}
	}
}